package com.ztf0337.mycurriculumdesign.ui.manager;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ztf0337.mycurriculumdesign.R;
import com.ztf0337.mycurriculumdesign.adapter.ParkLotAdapter;
import com.ztf0337.mycurriculumdesign.entity.Order;
import com.ztf0337.mycurriculumdesign.entity.ParkLot;
import com.ztf0337.mycurriculumdesign.service.ParkLotService;
import com.ztf0337.mycurriculumdesign.ui.activity.OrderDetailActivity;

import java.util.List;
import java.util.Objects;

public class MParkLotFragment extends Fragment {

    private Context context;
    private ParkLotService parkLotService;
    private ParkLotAdapter parkLotAdapter;
    private RecyclerView rvSpaceLot;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.m_fragment_park_lot, container, false);
        context = getContext();
        if (context == null) {
            return view;
        }
        parkLotService = new ParkLotService(context);

        TextView notData = view.findViewById(R.id.notData);
        rvSpaceLot = view.findViewById(R.id.rvSpaceLot);
        rvSpaceLot.setLayoutManager(new LinearLayoutManager(getContext()));

        List<ParkLot> allParkLot = parkLotService.getAllParkLot();

        if (!allParkLot.isEmpty()) {
            notData.setVisibility(GONE);
            parkLotAdapter = new ParkLotAdapter(allParkLot);
            rvSpaceLot.setAdapter(parkLotAdapter);
            parkLotAdapter.setOnItemClickListener((parkLot) -> {
                Intent intent = new Intent(context, MParkLotDetailActivity.class);
                intent.putExtra("id", parkLot.getId());
                context.startActivity(intent);
            });
        } else {
            notData.setVisibility(VISIBLE);
        }

        ImageView ic_add;
        ic_add = requireActivity().findViewById(R.id.ic_add);
        ic_add.setVisibility(VISIBLE);
        ic_add.setOnClickListener(v -> {
            Intent intent = new Intent(context, MUpdateParkLotActivity.class);
            intent.putExtra("from",1);
            context.startActivity(intent);
        });

        ImageView ic_Search = requireActivity().findViewById(R.id.ic_Search);
        EditText etSearch = requireActivity().findViewById(R.id.etSearch);
        etSearch.setText(""); // 清空内容

        etSearch.setHint("请输入停车场名");
        ic_Search.setOnClickListener(v -> {
            String search = etSearch.getText().toString().trim();
            if (search.isBlank()) {
                if (allParkLot.isEmpty()) {
                    Toast.makeText(context, "无停车场信息", Toast.LENGTH_SHORT).show();
                } else {
                    parkLotAdapter.updateData(allParkLot);
                }
                return;
            }
            List<ParkLot> parkLots = parkLotService.searchAllParkLots(search);
            if (parkLots.isEmpty()) {
                Toast.makeText(context, "无符合条件的停车场", Toast.LENGTH_SHORT).show();
            } else {
                parkLotAdapter.updateData(parkLots);
            }
        });

        return view;
    }
}
